from rest_framework import serializers
from rest_framework.validators import UniqueValidator

from apps.case.models import Case
from apps.case.case_result_models import CaseResult


class CaseSerializers(serializers.ModelSerializer):
    # creator = serializers.HiddenField(default=serializers.CurrentUserDefault())
    case_num = serializers.CharField(
        validators=[
            UniqueValidator(
                queryset=Case.objects.all(),
                message="用例编号已存在"
                )
            ],
        )

    class Meta:
        model = Case
        fields = "__all__"


class CaseListSerializers(serializers.ModelSerializer):

    class Meta:
        model = Case
        fields = '__all__'


class CaseResultSerializers(serializers.ModelSerializer):
    """
    用例结果
    """
    report_name = serializers.CharField(source='report.name', required=False, read_only=True)
    case_name = serializers.CharField(source='case.name', required=False, read_only=True)

    class Meta:
        model = CaseResult
        fields = ('id', 'name', 'report', 'case', 'case_name', 'report_name', 'log', 'result', 'add_time', 'edit_time')
